﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using System.Collections;

namespace SPMS.Utility
{

    /// <summary>
    /// 返回EnumDescription和对应的IntValue
    /// </summary>
    /// <remarks></remarks>
    /// <history>
    /// [FuQi]   1/6/2009 10:42 AM    Created
    /// </history>
    public class EnumDesc
    {
        /// <summary>
        /// EnumValue
        /// </summary>
        public int EnumValue;
        /// <summary>
        /// EnumDescription
        /// </summary>
        public string EnumDescription;
        /// <summary>
        /// Initializes a new instance of the <see cref="EnumDesc"/> class.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="desc">The desc.</param>
        /// <remarks></remarks>
        /// <history>
        /// [FuQi]   1/6/2009 10:43 AM    Created
        /// </history>
        public EnumDesc(int value, string desc)
        {
            EnumValue = value;
            EnumDescription = desc;
        }
        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        /// <remarks></remarks>
        /// <history>
        /// [FuQi]   1/6/2009 10:43 AM    Created
        /// </history>
        public override string ToString()
        {

            return EnumDescription;
        }
    }

    /// <summary>
    /// Enum辅助类，可以返回Enum元素的ArrayList，绑定到ListBox。
    /// </summary>
    /// <remarks>
    /// <para>Sample代码如下：</para>
    /// <code>
    /// //假设有一个ProductEnum对象，返回其名字。
    /// ArrayList descs;
    /// descs = GetDescriptions(typeof(ProductEnum));
    /// </code>
    /// </remarks>
    /// <history>
    /// [FuQi]   11/7/2008 11:00 AM    Created
    /// </history>
    public class EnumUtility
    {
        /// <summary>
        /// 辅助方法，一般不直接调用。
        /// 如果某个Enum对象的元素设置了Description，则返回Description，否则返回ToString。
        /// </summary>
        /// <param name="enumSubitem">Enum对象的元素</param>
        /// <returns>Description或者Enum元素的ToString</returns>
        /// <remarks></remarks>
        /// <history>
        /// [FuQi]   11/7/2008 11:01 AM    Created
        /// </history>
        public static string GetEnumDescription(Enum enumSubitem)
        {
            string strValue = enumSubitem.ToString();

            FieldInfo fieldinfo = enumSubitem.GetType().GetField(strValue);
            Object[] objs = fieldinfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (objs == null || objs.Length == 0)
            {
                return strValue;
            }
            else
            {
                DescriptionAttribute da = (DescriptionAttribute)objs[0];
                return da.Description;
            }

        }

        /// <summary>
        /// 取得某个Enum对象的所有元素的Description或者ToString
        /// </summary>
        /// <param name="enumType">Type of the enum.</param>
        /// <returns>Description组成的ArrayList</returns>
        /// <remarks>
        /// <para>Sample代码如下：</para>
        /// <code>
        /// //假设有一个ProductEnum对象，返回其名字。
        /// ArrayList descs;
        /// descs = GetDescriptions(typeof(ProductEnum));
        /// </code>
        /// 
        /// </remarks>
        /// <history>
        /// [FuQi]   11/7/2008 11:06 AM    Created
        /// </history>
        public static SortedList<int,string> GetValueDescriptions(Type enumType)
        {
            Array values;
            SortedList<int, string> ret = new SortedList<int, string>();

            if (enumType.IsEnum )
            {
                values = Enum.GetValues(enumType);

                for (int i = 0; i < values.Length; i++)
                {
                    ret.Add((int)values.GetValue(i), GetEnumDescription((Enum)values.GetValue(i)));
                }
            }
            return ret;
        }

        /// <summary>
        /// 返回值可以直接帮定到ListBox或者Combobox上。
        /// </summary>
        /// <param name="enumType">Type of the enum.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        /// <history>
        /// [FuQi]   1/6/2009 11:07 AM    Created
        /// </history>
        public static List<EnumDesc> GetValueDescriptionsEx(Type enumType)
        {
            Array values;
            List<EnumDesc> ret = new List<EnumDesc>();

            if (enumType.IsEnum)
            {
                values = Enum.GetValues(enumType);

                for (int i = 0; i < values.Length; i++)
                {
                    ret.Add(new EnumDesc((int)values.GetValue(i), GetEnumDescription((Enum)values.GetValue(i))));
                }
            }
            return ret;
        }

    }
}
